package com.qdxwx.manage.controller.article;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.article.ArticleService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.article.Article;
import com.qdxwx.models.article.ArticleQuery;
import com.qdxwx.models.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 资讯信息Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {
    private final String prefix = "article";
    @Autowired
    private ArticleService articleService;

    @GetMapping("/list.html")
    public String article() {
        return prefix + "/article-list";
    }

    /**
     * 查询资讯信息列表
     */
    @Permissions("article:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        ArticleQuery article = new ArticleQuery();
        article.setTitle(request.getParameter("title"));
        List<Article> list = articleService.getList(article, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(articleService.count(article));
        return rspData;
    }

    /**
     * 新增资讯信息
     */
    @GetMapping("/add.html")
    public String add(HttpServletRequest request, ModelMap mmap) {
        Article article = new Article();
        article.setSort(0);
        article.setPraiseCount(0);
        article.setViewCount(0);
        mmap.put("article", article);
        return prefix + "/article-add";
    }

    /**
     * 修改资讯信息
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int articleId = TKConvert.toInteger(request.getParameter("id"));
        if (articleId < 1) return error404;
        Article article = articleService.get(articleId);
        if (article == null) return error500;
        mmap.put("article", article);
        return prefix + "/article-edit";
    }

    /**
     * 修改保存资讯信息
     */
    @Permissions("article:edit")
    @Log(title = "保存资讯", businessType = BusinessType.UPDATE)
    @PostMapping("/save.json")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request) {
        String title = request.getParameter("title");
        if (TKString.isEmpty(title)) {
            return error("资讯标题不能为空");
        }
        String intro = request.getParameter("intro");
        if (TKString.isEmpty(intro)) {
            return error("资讯简介不能为空");
        }
        String cover = request.getParameter("cover");
        if (TKString.isEmpty(cover)) {
            return error("请上传资讯封面");
        }
        String content = request.getParameter("content");
        if (TKString.isEmpty(content)) {
            return error("资讯内容不能为空");
        }
        int articleId = TKConvert.toInteger(request.getParameter("articleId"));
        Article article;
        if (articleId > 0) {
            article = articleService.get(articleId);
            if (article == null) {
                return error("找不到对应的资讯信息");
            }
        } else {
            article = new Article();
        }
        article.setTitle(title);
        article.setIntro(intro);
        article.setCover(cover);
        article.setContent(content);
        int count;
        if (articleId > 0) {
            count = articleService.update(article);
        } else {
            count = articleService.insert(article);
        }
        return toAjax(count);
    }

    /**
     * 删除资讯信息
     */
    @Permissions("article:remove")
    @Log(title = "删除资讯", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if (id < 1) return error("缺少参数id");
        return toAjax(articleService.delete(id));
    }
}
